/*
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.application.CartFacadeImpl;
import com.deep.trade.application.exception.LayerControllerException;
import com.deep.trade.cart.dto.InDtoCart;
import com.deep.trade.cart.dto.InDtoCartDelete;
import com.deep.trade.cart.dto.OutDtoCartItem;
import com.deep.trade.common.CommonUtils;
import com.deep.trade.common.HttpResponsePayload;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;


@Controller
@RequestMapping(value = "/api/v1/cart")
public class CartController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private HttpResponseAssembler assembleService;

    private CartFacadeImpl cartServiceFacade;
    private AppRuntimeEvn appRuntimeEvn;

    @Autowired
    public CartController(
            CartFacadeImpl cartServiceFacade
            , AppRuntimeEvn appRuntimeEvn
            , HttpResponseAssembler assembleService
    ) {
        this.cartServiceFacade = cartServiceFacade;
        this.appRuntimeEvn = appRuntimeEvn;
        this.assembleService = assembleService;
    }

    /**
     * 添加sku到购物车
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = {RequestMethod.POST, RequestMethod.PUT}, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResponsePayload addSkuToCart(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestBody(required = false) InDtoCart inDtoCart
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");
        if (null == inDtoCart) throw LayerControllerException.argumentNotFound("empty body");

        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCart);
        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        cartServiceFacade.addSkuToCart(inDtoCart);
        return assembleService.happy(true);
    }

    /**
     * 从 购物车 删除一个sku
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.DELETE, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResponsePayload removeSkuFromCart(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestBody(required = false) InDtoCartDelete inDtoCartDelete
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");
        if (null == inDtoCartDelete) throw LayerControllerException.argumentNotFound("empty body");

        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCartDelete);

        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        cartServiceFacade.removeSkuFromCart(inDtoCartDelete);
        return assembleService.happy(true);
    }

    /**
     * 查看 购物车 列表
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public HttpResponsePayload cartListOfMember(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int size
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");

        logger.info("{}, tenantCode={}, token={}, page={}, size={}", CommonUtils.currentClassAndMethod(), tenantCode, token, page, size);
        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        PageInfo<OutDtoCartItem> pageInfo = cartServiceFacade.cartListOfMember(page, size);
        return assembleService.happy(pageInfo);
    }
}
